<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
abstract class AdminController extends WdkController
{
	function __construct()
	{
		parent::__construct();
		require_once PROJECT_PATH.'config/config.php';
		$this->config = $config;
		
		$this->session = Wdk::helper('AdminSession');
		
		$this->tpl = new Template_();
		$this->tpl->compile_dir = CACHE_PATH.'tpl_/_compile';
		$this->tpl->template_dir = BASE_PATH;
		$this->tpl->cache_dir = CACHE_PATH.'tpl_/_cache';
		$this->uri = Wdk::helper('Uri');
		
		require_once PROJECT_PATH.'core/menus.php';
		$this->tpl->assign("shop_menu", $menu);
		$this->tpl->assign("p_menu", (isset($this->uri['uri'][1]) ? $this->uri['uri'][1] : ''));
		$this->tpl->assign("s_menu", (isset($this->uri['uri'][2]) ? $this->uri['uri'][2] : ''));
		
		$this->layout = 'admin';
		$this->_init();
		
	}
	
	protected function _json($data=array(), $return=false)
	{
		if($return == true)
		{
			return htmlspecialchars(json_encode($data), ENT_QUOTES);
		}
		echo json_encode($data);
	}
	
	protected function _view($data = array(), $file='')
	{
		global $_SESSION;

		$this->tpl->define('view', $this->uri['path'].$this->uri['class'].'/'.$this->uri['method'].'.html');
		$this->tpl->assign($data);
		$this->tpl->assign('session', $_SESSION);
		$this->tpl->assign('default_js', $this->uri['path'].$this->uri['class'].'/js/'.$this->uri['method'].'.js');
		$this->tpl->assign('default_css', $this->uri['path'].$this->uri['class'].'/css/'.$this->uri['method'].'.css');
		$view = $this->tpl->fetch('view');
		
		$this->tpl->define('_CONTENTS_', '_layout/'.$this->layout.'.html');
		$this->tpl->assign('view', $view);
		$this->tpl->assign('PATH', $this->uri['path'].$this->uri['class'].'/');
		$output = $this->tpl->fetch('_CONTENTS_');
		$this->_parsing($output);
		print($output);
	}
	protected function _isLogin()
	{
		return $this->session->isLogin();
	}
	protected function _loginPage()
	{
		parent::_redirect("/admin/login/".base64_encode($_SERVER['REQUEST_URI']));
	}
	
	protected function _parsing(&$output)
	{
		if(preg_match_all("@{# plugin ([a-zA-Z0-9_-]*)(|\s[^\s]*) #}@siU", $output, $matches))
		{
			$texts = $matches[0];
			$plugins = $matches[1];
			$params = $matches[2];
			foreach($texts as $i=>$text)
			{
				$plugin = $plugins[$i];
				parse_str($params[$i], $param);
				$plg = Wdk::plugin($plugin, $param);
				$output = str_replace($text, $plg->exec(), $output);
				unset($text, $plugin, $param, $plg);
			}
			unset($texts, $plugins, $params);
		}
	}

	protected function _del($obj) 
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data	= array('error'=>true);
		$del_seq		= (array) post('del_seq');
		/****************************************************************************************************************************/
		if(parent::_isPost())
		{
			try
			{
				if(empty($del_seq) == false)
				{
					foreach($del_seq as $seq)
					{
						$obj->load($seq);
						$obj->delete();
					}
					$assign_data['error'] = false;
				}
			}
			catch(WException $e)
			{
				$assign_data['msg'] = $e->getMessage();
			}
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		exit(json_encode($assign_data));
		/****************************************************************************************************************************/		
	}
}